package com.example.demo.controller;

import com.example.demo.utils.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@RestController
public class DailyWork {

    private static final String WECHAT_DAILY_COUNT_URL="http://ewx.ucnho.com/attendance/attendance?user_id=";

    /**
     * @Author: liuzujie
     * @Desc: 获取当前人-当前月的数据
     * @Date: 2020/8/14 14:00
     */
    @RequestMapping(value = "/work/{user_id}", method = {RequestMethod.GET, RequestMethod.POST})
    public void getCurrnet(@PathVariable String user_id,HttpServletResponse response) {
        getAll(user_id,null,null,response);
    }

    /**
     * @Author: liuzujie
     * @Desc: 获取当前人-某年-某月的数据
     * @Date: 2020/8/14 14:01
     */
    @RequestMapping(value = "/work/{user_id}/{year}/{month}", method = {RequestMethod.GET, RequestMethod.POST})
    public void getAll(@PathVariable String user_id, @PathVariable String year, @PathVariable String month, HttpServletResponse response)  {
        String url = WECHAT_DAILY_COUNT_URL + user_id+"";
        if(!Objects.isNull(year)) url+="&year="+year;
        if(!Objects.isNull(month)) url+="&month="+month;
        String urlContent=HttpUtils.GetHttpHtml2String(url);
        //转换成Jsoup的Document元素后就可以随意操作你想要的数据来实现你的逻辑
        Document document = Jsoup.parse(urlContent);
        Elements element=document.getElementsByClass("summary");
        String s=element.get(0).childNode(1).outerHtml();
        System.out.println(s);
        try {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(urlContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
